package com.github.starry.fw.social.web.spider.wechat.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.github.starry.fw.social.web.spider.wechat.bean.Api;
import com.github.starry.fw.social.web.spider.wechat.bean.ApiParameter;
import com.github.starry.fw.social.web.spider.wechat.service.GetApiService;

@Component
public class GetApiServiceImpl1 implements GetApiService  {

    public List<Api> getApi(File file) throws Exception{

        List<Api> result = new ArrayList<Api>();

        Document doc = Jsoup.parse(file, "UTF-8");

        //Element bodyContent = doc.select("#bodyContent").first().child(0);
        String apiName = doc.select("#firstHeading").first().text();
        Element bodyContent = null;

        Iterator<Element> elementIterator = doc.select("#bodyContent").first().children().iterator();

        while(elementIterator.hasNext()){

            Element element = elementIterator.next();
            if(bodyContent == null) {
                bodyContent = new Document("aaa");
            }

                if(StringUtils.equalsIgnoreCase(element.tagName(), "h2")) {
                    Api api = getApiObject(apiName,bodyContent);
                    if(api != null) result.add(api);
                    apiName = element.text();
                    bodyContent = null;
                } else {
                    bodyContent.appendChild(element);
                }

        }

        if (bodyContent != null) {
            Api api = getApiObject(apiName, bodyContent);
            if (api != null)
                result.add(api);
        }

        return result;
    }

    private Api getApiObject(String apiName, Element root) {



        String method = getApiMethod(root.html());

        List<ApiParameter> params = searchApiParameter(root);

        System.out.println(method);

        return null;
    }

    private String getApiMethod (String html) {
        Pattern p = Pattern.compile("请求方式(:|：) *(?<HttpMethod>GET|POST)");
        Matcher m = p.matcher(html);
        while(m.find()) {
            String g = m.group("HttpMethod");
            return g;
        }

        return "";
    }

    private List<ApiParameter> searchApiParameter(Element root) {

        Pattern p = Pattern.compile("参数");

        Iterator<Element> elementIterator = root.children().iterator();

        while(elementIterator.hasNext()){
            Element next = elementIterator.next();

            if(p.matcher(next.html()).find() && next.nextElementSibling() != null && StringUtils.equalsIgnoreCase("table", next.nextElementSibling().tagName()) ) {
                return getParameter(next.nextElementSibling());
            }

            next = next.nextElementSibling();
        }


        return new ArrayList<ApiParameter>();
    }

    private List<ApiParameter> getParameter(Element table) {

        List<ApiParameter> paramList = new ArrayList<ApiParameter>();

        Iterator<Element> trIterator = table.select("tr").iterator();

        while(trIterator.hasNext()){
            List<String> itemValues = new ArrayList<String>();
            Iterator<Element> tdIterator = trIterator.next().select("td").iterator();
            while(tdIterator.hasNext()){
                itemValues.add(tdIterator.next().html());
            }

            if(CollectionUtils.isEmpty(itemValues) || itemValues.size() != 3) {
                continue;
            }

            paramList.add(new ApiParameter(itemValues.get(0),itemValues.get(1),itemValues.get(2)));
        }

        return paramList;
    }


}